//
//  DetailController.swift
//  addressBook
//
//  Created by ZhangXu on 2017/12/20.
//  Copyright © 2017年 zhangXu. All rights reserved.
//

import UIKit

class DetailController: UITableViewController {
    @IBOutlet weak var nameText: UITextField!
    @IBOutlet weak var phoneText: UITextField!
    @IBOutlet weak var titleText: UITextField!
    var person:Person?
    
    //完成回调属性 闭包是可选的
    var completionCallBack:(() -> ())?
    override func viewDidLoad() {
        super.viewDidLoad()

       
        if person != nil{
            
            nameText.text = person?.name
            phoneText.text = person?.phone
            titleText.text = person?.title
        }
        
        
        
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

 
    @IBAction func saveBt(_ sender: Any) {
        //判断person是否为nil
        
        if person == nil {
            
            person = Person()
   
        }
        
        
        person?.name = nameText.text
        person?.phone = phoneText.text
        person?.title = titleText.text
        
        //执行闭包回调
        
        // ! 强行解包 ?可选解包 如果闭包为nil 什么也不干
        completionCallBack?()
        
        
        navigationController?.popViewController(animated: true)
        
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
